<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<div class="float_win" style="width: 643px;">
	<div class="con">
		<div id="div_user"  class="cyz bor flo_l" style="height: 365px;">
			<div class="table_list_tab" style="padding-left: 15px;margin-top: 5px;margin-bottom: 2px;">
				<a href="javascript:void(0);" onclick="dirUserRelate();" id="usual" class="active" style="display: inline-block;width: 90px;text-align: center;margin:0px;">常用联系人</a>
				<a href="javascript:void(0);" onclick="dirSearch();" id="showOrgTree" class="" style="display: inline-block;width: 90px;text-align: center;margin:0px;">组织机构树</a>
			</div>
			
			<div id="user_post_list" class="iconspage" style="border:0px;width:280px;">
				<form class="form-horizontal search"  id="input_name_form" method="post" action="dirOrgUserAndPostJson.action" onsubmit="return dirOrgUserAndPost();" style="display: inline;display: none;">
					<div class="title iconspage" style="width: 262px;">
						<%-- 人/岗位名：--%>
						<input type="text" id="input_name" name="name" title="姓名" style="width: 140px;margin-bottom: 0;" maxlength="10" value=""/>
						<button class="btn green" caption="查询" onclick="$('#input_name_form').submit()">查询</button>
						<span style="float: right;margin-right: 5px;margin-left: 0px;">
							<i onclick="dirOrgTree()" title="组织机构树" style="cursor: pointer;" class="icon_organizing"></i>
							<%-- <i id="add_sel" title="选择" class="icon_recycle_right"></i> --%>
						</span>
					</div>
				</form>
				<div style="height:328px;overflow: auto;" class="show">
					<table style="margin-left:5px;width:98%;">
						<tbody id="dirList">
						</tbody>
					</table>
				</div>
			</div>
		</div>
		<form class="form-horizontal"  id="wf_send_form" method="post" action="wfSend.action" onsubmit="return submitSelUsers(this, dialogAjaxDone);" style="width: 362px;">
			<%--仅为实现流程变量传递给发送操作 --%>
			<div class="mid_pic bor flo_l"><i id="add_sel"></i></div>
			<div id="sel_div_user" class="selcyz bor flo_l" style="float: right;">
				<div class="title iconspage" style="border-bottom-style: double;">
					<%-- <i class="icon_closed" title="删除选择" id='del_sel'></i> --%>
					已选择用户<span id="selected" style="color: red;"></span>
					<span style="float: right;margin-right: 5px" id="div_buttons_up">
						<i class="icon_arrow_up" title="上移" id='up_sel'></i>
						<i class="icon_arrow_down" title="下移" id='down_sel'></i>
					</span>
				</div>
				<div id="sel_user_post_list" style="height:330px;border:0px;width:280px;overflow:auto;">
					<table style="margin-left:5px;width:98%;">
						<tbody id="selList">
						</tbody>
					</table>
				</div>
			</div>
		</form>
		<div class="clear" style="margin-top: -3px;"></div>
		<div class="set-btn" data-spy="affix" data-offset-top="200">
			<button id="focusSubmit" class="btn green" caption="确定">确定</button>
			<a class="btn reset" href="javascript:;" onclick="$css.closeDialog();">取消</a>
		</div>
	</div>
</div>
<script type="text/javascript">
var showActor = true;
var showPost  = true;
var upperLimit = -1;
var lowerLimit = -1;
var user_post_array = [];
var orgId = null ;
var user_setting = {
	data : {
		simpleData : {
			enable : true
		}
	},
	async: {
		enable: true ,
		url:"getOrgTreeJson.action",
		autoParam:["id=parentId"],
		type:"post"
	},
	callback : {
		onClick : org_onClick
	}
};
var user_zNodes=[];

/**
 * 异步加载人员岗位数据 
 **/
function org_onClick(e, treeId, treeNode) {
	if(!treeNode || !treeNode.id)
	{ 
		$css.alert('请先选择机构部门!');
		return false;
	}
	orgId = treeNode.id;  	
	performerShow_onClick();
}
function performerShow_onClick() {	
	if(!orgId)
	{
		 //alert('请选择机构!');
		 return false;
	}
	var $orgUserPostList = $('#div_user #user_post_list').find("#dirList");		 
	//获取机构关联的岗位和人	
	$.post("getOrgUserAndPostJson.action",{"orgId" : orgId,'showPost':showPost,'showActor':showActor},function(json){
		if(json.result=='0'){
			$('#input_name').val("");
			var items = json.info;
			$orgUserPostList.empty();
			if(isnull(items)){
				$orgUserPostList.prepend("<tr><td style='text-align: center;color: gray;'>此机构下为空,请重新选择!</td></tr>");
			}else{
				for(var idx in items){
					var item=items[idx];
					var itemId=item.typeid;
					var relateFlag = item.relateFlag;
					showDirList(item,itemId,relateFlag);
				}
			}
			$css.closeDialog();
		}else
			alert(json.msg);
	},'json');
}
/****
 * 显示组织机构选项卡
 */
function dirSearch(){
	var $orgUserPostList = $('#div_user #user_post_list').find("#dirList");
	$(".search").show();
	$(".show").attr("style","height:290px;overflow: auto;");
	$("#usual").removeClass('active');
	$("#showOrgTree").addClass('active');
	$orgUserPostList.empty();
}
/****
 * 显示组织机构树弹出框
 */
function dirOrgTree(){
	var parent = $.dialog.focus;
	var current = $css.openDialog({
		title:"组织机构树",
		url:'dirOrgTree.action',
		rel:"dirOrgTree",
		lock:true
	});
	$.dialog.focus.config.parent = parent;
}

/****
 * 查询常用联系人
 */
function dirUserRelate(){
	var $orgUserPostList = $('#div_user #user_post_list').find("#dirList");
	$(".search").hide();
	$(".show").attr("style","height:328px;overflow: auto;");
	$orgUserPostList.empty();
	$.post("dirUserRelate.action",{'showPost':showPost,'showActor':showActor},function(data){
		if(data.result=='0'){
			$('#input_name').val("");
			var items = data.info;
			if(isnull(items)){
				$orgUserPostList.prepend("<tr><td style='text-align: center;color: gray;'>暂无常用联系人!</td></tr>");
			}else{
				for(var idx in items){
					var item=items[idx];
					var itemId=item.typeid;
					showDirList(item,itemId,"true");
				}
			}
			$("#usual").addClass('active');
			$("#showOrgTree").removeClass('active');
		}else{
			alert(data.msg);
		}
	},'json');
}

/****
 * 查询人/岗位名称
 */
function dirOrgUserAndPost(){
	var $orgUserPostList = $('#div_user #user_post_list').find("#dirList");
	var name = $('#input_name').val();
	if(isnull(name)){
		if(showActor){
			$.dialog.tip('<div style="min-width:230px;text-align:center;">请输入姓名!</div>');
		}else if(showPost){
			$.dialog.tip('<div style="min-width:230px;text-align:center;">请输入岗位名!</div>');
		}
		return false;
	}
//	$.dialog.tip('<div style="min-width:230px;text-align:center;">'+name+'</div>');
	$.post("dirOrgUserAndPostJson.action",{"name" : name,'showPost':showPost,'showActor':showActor},function(data){
		if(data.result=='0'){
			var items = data.info;
			$orgUserPostList.empty();
			if(isnull(items)){
				$orgUserPostList.prepend("<tr><td style='text-align: center;color: gray;'>没有符合查询条件的结果!</td></tr>");
			}else{
				for(var idx in items){
					var item=items[idx];
					var itemId=item.typeid;
					var relateFlag = item.relateFlag;
					showDirList(item,itemId,relateFlag);
				}
			}
		}else{
			alert(data.msg);
		}
	},'json');
	return false;
}

/****
 * 添加常用联系人
 */
function addUserRelate(event,value){
	if($(value).attr("value").split("/")[0] == "ACTOR"){
		var pars = {relateId:$(value).attr("value").split("/")[3],relateType:"1",relateOrgId:$(value).attr("value").split("/")[1]};
	}else if($(value).attr("value").split("/")[0] == "GROUP"){
		var pars = {relateId:$(value).attr("value").split("/")[2],relateType:"2",relateOrgId:$(value).attr("value").split("/")[1]};
	}
	$.post('addUserRelate.action',pars,function(data){
		switch(data.result){
			case 0:
				$(value).attr("onclick","delUserRelate(event,this)");
				$(value).attr("class","icon_star");
				$(value).attr("title","删除常用联系人");
				break;
			default:
				$css.alert(data.msg);
				break;
		}
	},'json');
	event.cancelBubble = true;
}

/****
 * 删除常用联系人
 */
function delUserRelate(event,value){
	if($(value).attr("value").split("/")[0] == "ACTOR"){
		pars = {relateId:$(value).attr("value").split("/")[3],relateType:"1",relateOrgId:$(value).attr("value").split("/")[1]};
	}else if($(value).attr("value").split("/")[0] == "GROUP"){
		pars = {relateId:$(value).attr("value").split("/")[2],relateType:"2",relateOrgId:$(value).attr("value").split("/")[1]};
	}
	$.post('delUserRelate.action',pars,function(data){
		switch(data.result){
			case 0:
				$(value).attr("onclick","addUserRelate(event,this)");
				$(value).attr("class","icon_star_empty");
				$(value).attr("title","添加常用联系人");
				break;
			default:
				$css.alert(data.msg);
				break;
		}
	},'json');
	event.cancelBubble = true;
}

/****
 * 判断是否已选中
 */
function hasOption(targetList,vid){
	var options = $('#sel_div_user #sel_user_post_list #selList').children("tr");
	var count = options.length;
	for(var i=0;i<count;i++) 
	{ 	
	    if(options.eq(i).attr("rel") == vid) 
	    { 
	        return true;
	    } 
	} 
	return false;
}

/****
 * 左侧列表单/双击事件
 **/
function dir_tr_click(tr){
	var $dialog = $("#dirList");
	$dialog.find('.winli_sel').removeClass('winli_sel');
	$(tr).addClass('winli_sel');
}
function dir_tr_dblclick(tr){
	var tr = $(tr);
	add_performer(tr);
	$("#focusSubmit").focus();
}

/****
 * 右侧列表单/双击事件
 **/
function sel_tr_click(tr){
	var $dialog = $("#selList");
	$dialog.find('.winli_sel').removeClass('winli_sel');
	$(tr).addClass('winli_sel');
	//将tr信息放入隐藏域
}
function sel_tr_dblclick(tr){
	var tr = $(tr);
	remove_performer(tr);
	$("#focusSubmit").focus();
}

/****
 * 选择
 */
function add_performer(tr){
	var targetList = $("#sel_div_user #sel_user_post_list");
	//尚未选择即可添加进去
	 var id="",value="",name="";
	 if(tr.jquery){
	 	id = tr.attr("rel");
	 	value = tr.attr("value");
	 	name = tr.attr("title");
	 }else{
	 	id = tr.id;
	 	name = tr.name;
	 }
	if(!hasOption(targetList,id)){
		var $selectList = $('#sel_div_user #sel_user_post_list').find("#selList");
		var selHtml = "<tr rel='"+ id +"' value='" + value + "' onclick='sel_tr_click(this);' ondblclick='sel_tr_dblclick(this);'><td style='cursor:default;padding-left: 10px;'>" + name + "</td></tr>"
		$selectList.append(selHtml);
		var selected = "(" + $('#sel_div_user #sel_user_post_list #selList').children("tr").length + ")";
		$('#selected').html(selected);
		//默认选择第一个
		//if($("#div_user #user_post_list option").length>0){
		//$("#div_user #user_post_list option").eq(0).attr("selected",true);
		//}
	}else{
		$.dialog.tip('<div style="min-width:230px;text-align:center;">"' + name + '"已经被选中!</div>');
	}
}

/****
 * 取消选择
 */
function remove_performer(tr){
	$(tr).remove();
	var selected = "(" + $('#sel_div_user #sel_user_post_list #selList').children("tr").length + ")";
	$('#selected').html(selected);
	//默认选择第一个
	//if($("#sel_div_user #sel_user_post_list option").length>0){
	//	$("#sel_div_user #sel_user_post_list option").eq(0).attr("selected",true);
	//}
}

/****
 * 提交选择的用户
 */
function submitSelUsers(form,callback){
	var workitemId = $(form).find("input[name='workitemId']");
	if(workitemId.val()==''){
		 $css.alert('需要指定任务工单编号!');
		 return false;
	}
	var ids='';
	var options = $('#sel_div_user #sel_user_post_list #selList').children("tr");
	var count = options.length;
	for(var i=0;i<count;i++){
		if(ids == '')
    		ids += options.eq(i).attr('value');
    	else
    		ids += ',' + options.eq(i).attr('value');
	}
	//当下一环节未设置执行者时需要校验
    if((ids == '')){
		 $css.alert('请选择参与者!');
		 return false;
	}
	if(ids.length>0){
		var actids=ids.split(",");
		var size = actids.length;		 
		if(size < lowerLimit){
			$css.alert('请至少选择'+lowerLimit+'人!');
			return false;
		}
		if(size > upperLimit){
			$css.alert('超过参与者上限:'+upperLimit+'人!');
			return false;
		}
		$(form).find("#nextActors").val(ids);
	}
	
	// 修改会签传参数的方式问题           by xingzhc 2014-12-16
	if(lowerLimit > 1 ||upperLimit > lowerLimit){
	    var signMark =$("input[name='countersignMarkVar']:checked").val();
	   $(form).find("#countersignMark").val(signMark);
	}

	$css.closeTab();		 
	return $action.submit(form, callback);
}
    

/****
 * 显示左侧列表
 **/
function showDirList(item,itemId,relateFlag){
	var $orgUserPostList = $('#div_user #user_post_list').find("#dirList");
	var optHtml = "";
	var name = item.name.split('[')[0];
	if (!isnull(name) && name.length > 15) {
		name = name.substring(0, 15)+ "...";
	}
	if(relateFlag == "true"){
		optHtml = "<tr onclick='dir_tr_click(this);' ondblclick='dir_tr_dblclick(this);' rel='"+ item.id +"' value='" + item.typeid + "' title='"+ item.name.split('[')[0] +"'><td title='"+ item.name +"' style='cursor:default;padding-left: 10px;'>" + name + "</td><td style='width:40px;text-align:right;padding-right: 5px;'><i onclick='delUserRelate(event,this)' style='cursor: pointer;' ondblclick='event.cancelBubble = true;' value='" + itemId + "' title='删除常用联系人' class='icon_star'></i></td></tr>"
	}else{
		optHtml = "<tr onclick='dir_tr_click(this);' ondblclick='dir_tr_dblclick(this);' rel='"+ item.id +"' value='" + item.typeid + "' title='"+ item.name.split('[')[0] +"'><td title='"+ item.name +"' style='cursor:default;padding-left: 10px;'>" + name + "</td><td style='width:40px;text-align:right;padding-right: 5px;'><i onclick='addUserRelate(event,this)' style='cursor: pointer;' ondblclick='event.cancelBubble = true;' value='" + itemId + "' title='添加常用联系人' class='icon_star_empty'></i></td></tr>"
	}
	$orgUserPostList.prepend(optHtml);
}

/****
 * 初始化页面
 **/
function initPerformer(){
	var $orgUserPostList = $('#div_user #user_post_list').find("#dirList");
	var items = user_post_array;
	$orgUserPostList.empty();
	if(isnull(items)){
		$orgUserPostList.prepend("<tr><td style='text-align: center;color: gray;'>暂无常用联系人!</td></tr>");
	}else{
		for(var idx in user_post_array){
			var item=items[idx];
			var itemId=item.typeid;
			showDirList(item,itemId,"true");
		}
	}
}
$(function(){
	//上移
	$('#up_sel').on("click",function(){
		var $opt = $("#wf_send_form #selList").find(".winli_sel");
	 	if($opt.length != 0 && $opt.prev().length == 0)
	 	{
	 		$.dialog.tip('<div style="min-width:230px;text-align:center;">已排在首位不能上移!</div>');
	 		//$css.alert('顶级元素不能上移!');
	 		return;
	 	}
	 	var newTr = $opt.clone();
	 	newTr.insertBefore($opt.prev());
	 	$opt.remove();
	});
     
	//下移
    $('#down_sel').on("click",function(){
    	var $opt = $("#wf_send_form #selList").find(".winli_sel");
		if($opt.length != 0 && $opt.next().length == 0)
   	 	{
			$.dialog.tip('<div style="min-width:230px;text-align:center;">已排在末尾不能下移!</div>');
   	 		//$css.alert('末尾元素不能下移!');
   	 		return;
   	 	}
		var newTr = $opt.clone();
	 	newTr.insertAfter($opt.next());
	 	$opt.remove();
	});
	//保存按钮
	$('#focusSubmit',$css.focusDialog()).click(function(){
		var nodes = [];
		var data = $.dialog.focus.data;
		$.each($('#sel_div_user #sel_user_post_list #selList').children("tr"),function(i,v){
			var $v = $(v);
			nodes.push({id:$v.attr('rel'),name:$v.text().trim()})
		});
		if(data.save(nodes)){
			$css.closeDialog();
		}
	});
	//常用联系人
	$("#usual",$css.focusDialog()).click();
	//已选用户
	var nodes = [];
	var data = $.dialog.focus.data;
	if(data){
		nodes = $.dialog.focus.data.settings.checkNodes||[];
	}
	$.each(nodes,function(i,v){
		add_performer(v);
	});
})
</script>